package com.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.bean.Dish;
import com.zhao.bean.Setmeal;
import com.zhao.bean.ShoppingCart;
import com.zhao.config.BaseContext;
import com.zhao.dao.SetmealDao;
import com.zhao.dao.ShoppingCartDao;
import com.zhao.dao.dishDao;
import com.zhao.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author HK
 */

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Autowired
    private com.zhao.dao.dishDao dishDao;

    @Autowired
    private SetmealDao setmealDao;

    /**
     * 购物车和菜单页面中商品的删除
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart delete(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.get());
        if (shoppingCart.getDishId() != null) {

            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getUserId, shoppingCart.getUserId()).eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart shoppingCart1 = shoppingCartDao.selectOne(lqw);
            if (shoppingCart1.getNumber() > 1) {
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                Dish dish = dishDao.selectById(shoppingCart.getDishId());
                BigDecimal dishPrice = dish.getPrice();
                int dishPriceInt = dishPrice.intValue();
                int amount = shoppingCart1.getAmount().intValue() - dishPriceInt / 100;
                BigDecimal bigDecimal = new BigDecimal(amount);
                shoppingCart1.setAmount(bigDecimal);

                shoppingCartDao.update(shoppingCart1, lqw);
                return shoppingCart1;
            } else {
                shoppingCartDao.delete(lqw);
                shoppingCart1.setNumber(0);
                return shoppingCart1;
            }
        } else {
            Setmeal setmeal = setmealDao.selectById(shoppingCart.getSetmealId());
            BigDecimal setmealPrice = setmeal.getPrice();
            int setmealPriceInt = setmealPrice.intValue();
            LambdaQueryWrapper<ShoppingCart> lq = new LambdaQueryWrapper<>();
            lq.eq(ShoppingCart::getUserId, shoppingCart.getUserId()).eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart shoppingCart2 = shoppingCartDao.selectOne(lq);
            if (shoppingCart2.getNumber() > 1) {
                shoppingCart2.setNumber(shoppingCart2.getNumber() - 1);
                int amount = shoppingCart2.getAmount().intValue() - setmealPriceInt / 100;
                BigDecimal bigDecimal = new BigDecimal(amount);
                shoppingCart2.setAmount(bigDecimal);
                shoppingCartDao.update(shoppingCart2, lq);
                return shoppingCart2;
            } else {
                shoppingCartDao.delete(lq);
                shoppingCart2.setNumber(0);
                return shoppingCart2;
            }
        }
    }
}
